package com.ruoyi.classes.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.classes.domain.Classes;
import com.ruoyi.classes.mapper.ClassesMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.student.domain.StuInfo;
import com.ruoyi.student.mapper.StuInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.classes.mapper.ClassStudentRelationshipMapper;
import com.ruoyi.classes.domain.ClassStudentRelationship;
import com.ruoyi.classes.service.IClassStudentRelationshipService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 学生班级Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-10
 */
@Service
public class ClassStudentRelationshipServiceImpl implements IClassStudentRelationshipService
{
    @Resource
    private ClassStudentRelationshipMapper classStudentRelationshipMapper;
    @Resource
    private StuInfoMapper stuInfoMapper;
    @Resource
    private ClassesMapper classesMapper;


    /**
     * 查询学生班级
     *
     * @param relationshipId 学生班级主键
     * @return 学生班级
     */
    @Override
    public ClassStudentRelationship selectClassStudentRelationshipByRelationshipId(Long relationshipId)
    {
        return classStudentRelationshipMapper.selectClassStudentRelationshipByRelationshipId(relationshipId);
    }

    /**
     * 查询学生班级列表
     *
     * @param classStudentRelationship 学生班级
     * @return 学生班级
     */
    @Override
    public List<ClassStudentRelationship> selectClassStudentRelationshipList(ClassStudentRelationship classStudentRelationship)
    {
        return classStudentRelationshipMapper.selectClassStudentRelationshipList(classStudentRelationship);
    }

    /**
     * 新增学生班级
     *
     * @param classStudentRelationship 学生班级
     * @return 结果
     */
    @Override
    public int insertClassStudentRelationship(ClassStudentRelationship classStudentRelationship)
    {
        return classStudentRelationshipMapper.insertClassStudentRelationship(classStudentRelationship);
    }

    /**
     * 修改学生班级
     *
     * @param classStudentRelationship 学生班级
     * @return 结果
     */
    @Override
    public int updateClassStudentRelationship(ClassStudentRelationship classStudentRelationship)
    {
        return classStudentRelationshipMapper.updateClassStudentRelationship(classStudentRelationship);
    }

    /**
     * 批量删除学生班级
     *
     * @param relationshipIds 需要删除的学生班级主键
     * @return 结果
     */
    @Override
    public int deleteClassStudentRelationshipByRelationshipIds(Long[] relationshipIds)
    {
        return classStudentRelationshipMapper.deleteClassStudentRelationshipByRelationshipIds(relationshipIds);
    }

    /**
     * 删除学生班级信息
     *
     * @param relationshipId 学生班级主键
     * @return 结果
     */
    @Override
    public int deleteClassStudentRelationshipByRelationshipId(Long relationshipId)
    {
        return classStudentRelationshipMapper.deleteClassStudentRelationshipByRelationshipId(relationshipId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertClassStudentRelationshipList(List<ClassStudentRelationship> classStudentRelationships) {
        for (ClassStudentRelationship classStudentRelationship : classStudentRelationships) {
            ClassStudentRelationship classStudentRelationship1 = classStudentRelationshipMapper.selectClassStudentRelationshipByStuId(classStudentRelationship.getStudentId());
            if(classStudentRelationship1==null){
                classStudentRelationshipMapper.insertClassStudentRelationship(classStudentRelationship);
                Classes classes = classesMapper.selectClassesByClassId(classStudentRelationship.getClassId());
                StuInfo stuInfo = new StuInfo();
                stuInfo.setStudentId(String.valueOf(classStudentRelationship.getStudentId()));
                stuInfo.setClassName(classes.getClassName());
                stuInfo.setClassId(String.valueOf(classes.getClassId()));
                int i = stuInfoMapper.updateStuInfo(stuInfo);
                System.out.println(i);
            }
        }
        return AjaxResult.success();
    }

    @Override
    public List<StuInfo> getStuList(Long classId) {
        List<StuInfo> stuInfos = new ArrayList<>();
        ClassStudentRelationship classStudentRelationship = new ClassStudentRelationship();
        classStudentRelationship.setClassId(classId);
        List<ClassStudentRelationship> classStudentRelationships = classStudentRelationshipMapper.selectClassStudentRelationshipList(classStudentRelationship);
        if (classStudentRelationships.toArray().length == 0) {
            return stuInfos;
        }

        for (ClassStudentRelationship studentRelationship : classStudentRelationships) {
            StuInfo stuInfo = stuInfoMapper.selectStuInfoByStudentId(String.valueOf(studentRelationship.getStudentId()));
            stuInfos.add(stuInfo);
        }
        return stuInfos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteByStuIdAndClassId(List<ClassStudentRelationship> relationships) {
        StuInfo stuInfo = new StuInfo();
        for (ClassStudentRelationship relationship : relationships) {
            stuInfo.setStudentId(String.valueOf(relationship.getStudentId()));
            stuInfoMapper.deleteClass(stuInfo);
            classStudentRelationshipMapper.deleteByStuIdAndClassId(relationship.getStudentId(),relationship.getClassId());
        }
        return AjaxResult.success();
    }
}
